package com.hui.admin.controller;

import com.hui.admin.annotation.Log;
import com.hui.admin.common.result.Result;
import com.hui.admin.common.result.Results;
import com.hui.admin.dto.request.UserForgetPasswordRequestDTO;
import com.hui.admin.dto.request.UserLoginRequestDTO;
import com.hui.admin.dto.request.UserRegisterRequestDTO;
import com.hui.admin.dto.response.UserForgetPasswordResponseDTO;
import com.hui.admin.dto.response.UserLoginResponseDTO;
import com.hui.admin.service.UserService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 认证控制层
 */
@Slf4j
@RestController
@RequestMapping("/api/auth")
public class AuthController {

    private final UserService userService;

    public AuthController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 生成验证码
     */
    @GetMapping("/getCaptcha")
    public void getShearCaptcha(HttpServletResponse response) {
        userService.getShearCaptcha(response);
    }

    /**
     * 登录
     */
    @Log("登录")
    @PostMapping("/login")
    public Result<UserLoginResponseDTO> login(@RequestBody UserLoginRequestDTO request) {
        return Results.success(userService.login(request));
    }

    /**
     * 注册
     */
    @Log("注册")
    @PostMapping("/register")
    public Result<Void> register(@RequestBody UserRegisterRequestDTO request) {
        userService.register(request);
        return Results.success();
    }

    /**
     * 忘记密码
     */
    @PostMapping("/forget-password")
    public Result<UserForgetPasswordResponseDTO> forgetPassword(@RequestBody UserForgetPasswordRequestDTO request) {
        return Results.success(userService.forgetPassword(request));
    }

    /**
     * 忘记密码（确认）
     */
    @Log("忘记密码")
    @PostMapping("/forget-password-confirm")
    public Result<Void> forgetPasswordConfirm(@RequestBody UserForgetPasswordRequestDTO request) {
        userService.forgetPasswordConfirm(request);
        return Results.success();
    }
}
